import pygame

# 植物类
class Plant:
    def __init__(self, surface, font, region_left, region_top, grid_width, grid_height, row, col, card_attr, color, head_color, health = 20):
        self.surface = surface
        self.font = font
        self.region_left = region_left # 区域左端
        self.region_top = region_top # 区域上端
        self.grid_width = grid_width # 网格宽度
        self.grid_height = grid_height # 网格高度
        self.row = row # 行号
        self.col = col # 列号
        self.card_attr = card_attr # 卡牌属性
        self.health = health # 生命值
        self.max_health = self.health # 最大生命值
        self.attack_cooldown = 0 # 攻击冷却
        self.color = color # 颜色
        self.size = 30 # 大小
        self.head_color = head_color # 头部颜色
        self.left = self.col * self.grid_width + self.region_left
        self.top = self.row * self.grid_height + self.region_top
        self.right = self.left + self.grid_width
        self.bottom = self.top + self.grid_height
    
    def update(self):
        if self.attack_cooldown > 0:
            self.attack_cooldown -= 1
    
    def draw(self, surface):
        x = self.col * self.grid_width + self.grid_width // 2 + self.region_left
        y = self.row * self.grid_height + self.grid_height // 2 + self.region_top
        # 绘制射手身体
        if self.head_color == None:
            pygame.draw.circle(surface, self.color, (x, y), self.size)
        else:
            pygame.draw.circle(surface, self.color, (x, y - 10), self.size - 5)
            pygame.draw.line(surface, self.color, (x, y + 15), (x, y + 30), 3)
            pygame.draw.line(surface, self.color, (x - 20, y + 30), (x + 20, y + 30), 3)
        # 绘制射手头部
        if self.head_color != None:
            pygame.draw.circle(surface, self.head_color, (x + 20, y), 15)
        # 绘制血条
        health_width = 60 * (self.health / self.max_health)
        pygame.draw.rect(surface, (255, 0, 0), (x - 30, y - 40, 60, 5))
        pygame.draw.rect(surface, (0, 255, 0), (x - 30, y - 40, health_width, 5))
        # 血条显示文字
        blood_text = self.font.render(f"{self.health} / {self.max_health}", True, (0, 0, 0))
        text_rect = blood_text.get_rect()
        text_rect.centerx = x
        text_rect.centery = y - 38
        self.surface.blit(blood_text, text_rect)
    
    def can_attack(self):
        return self.attack_cooldown == 0
